package com.trassion.flutterhybirddemo

import android.content.Context
import android.view.View
import android.widget.TextView
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.platform.PlatformView

class MyFlutterView(context: Context, messenger: BinaryMessenger, viewId: Int, args: Map<String, Any>?) : PlatformView,MethodChannel.MethodCallHandler {

    val textView: TextView = TextView(context)
    var methodChannel: MethodChannel = MethodChannel(messenger, "com.trassion.flutterhybirddemo.MyFlutterView")

    init {
        if (args != null && args.containsKey("text")) {
            textView.text = args["text"] as String
        } else {
            textView.text = "我是Android View"
        }
        methodChannel.setMethodCallHandler(this)
    }

    override fun getView(): View {

        return textView
    }

    override fun dispose() {
        TODO("Not yet implemented")
    }

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        if (call.method == "setText") {
            val name = call.argument<String>("name")
            val age = call.argument<Int>("age")
            textView.text =  name + age
        }else if (call.method == "getData") {
            result.success("我是Android View")
        }
    }
}
